import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from dateutil import parser

df_ferrara = pd.read_csv('WeatherData/ferrara_270615.csv')
df_milano = pd.read_csv('WeatherData/milano_270615.csv')
df_mantova = pd.read_csv('WeatherData/mantova_270615.csv')
df_ravenna = pd.read_csv('WeatherData/ravenna_270615.csv')
df_torino = pd.read_csv('WeatherData/torino_270615.csv')
df_asti = pd.read_csv('WeatherData/asti_270615.csv')
df_bologna = pd.read_csv('WeatherData/bologna_270615.csv')
df_piacenza = pd.read_csv('WeatherData/piacenza_270615.csv')
df_cesena = pd.read_csv('WeatherData/cesena_270615.csv')
df_faenza = pd.read_csv('WeatherData/faenza_270615.csv')

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

def stage1():
    # 读取我们要分析的温度和日期数据
    # y1 = df_milano['temp']
    # x1 = df_milano['day']
    y1 = df_ravenna['temp']
    x1 = df_ravenna['day']
    y2 = df_faenza['temp']
    x2 = df_faenza['day']
    y3 = df_cesena['temp']
    x3 = df_cesena['day']
    y4 = df_milano['temp']
    x4 = df_milano['day']
    y5 = df_asti['temp']
    x5 = df_asti['day']
    y6 = df_torino['temp']
    x6 = df_torino['day']

    # 把日期从 string 类型转化为标准的 datetime 类型
    # day_milano = [parser.parse(x) for x in x1]
    day_ravenna = [parser.parse(x) for x in x1]
    day_faenza = [parser.parse(x) for x in x2]
    day_cesena = [parser.parse(x) for x in x3]
    day_milano = [parser.parse(x) for x in x4]
    day_asti = [parser.parse(x) for x in x5]
    day_torino = [parser.parse(x) for x in x6]

    # 调用 subplots() 函数，fig 是图像对象，ax 是坐标轴对象
    fig, ax = plt.subplots()

    # 调整 x 轴坐标刻度，使其旋转 70 度，方便查看
    plt.xticks(rotation=70)

    # 设定时间的格式
    hours = mdates.DateFormatter('%H:%M')

    # 设定 x 轴显示的格式
    ax.xaxis.set_major_formatter(hours)

    # 画出图像
    # day_milano 是 x 轴数据，y1 是 y 轴数据，'r' 代表的是 'red' 红色
    # ax.plot(day_milano, y1, 'r')
    # 需要画出三根线，所以需要三组参数，'g' 代表的是 'green' 绿色
    ax.plot(day_ravenna, y1, 'r', day_faenza, y2, 'r', day_cesena, y3, 'r')
    ax.plot(day_milano, y4, 'g', day_asti, y5, 'g', day_torino, y6, 'g')

    plt.show()

def stage2():
    # dist 是一个装城市距离海边距离的列表
    dist = [
        df_ravenna['dist'][0],
        df_cesena['dist'][0],
        df_faenza['dist'][0],
        df_ferrara['dist'][0],
        df_bologna['dist'][0],
        df_mantova['dist'][0],
        df_piacenza['dist'][0],
        df_milano['dist'][0],
        df_asti['dist'][0],
        df_torino['dist'][0]
    ]

    # temp_max 是一个存放每个城市最高温度的列表
    temp_max = [
        df_ravenna['temp'].max(),
        df_cesena['temp'].max(),
        df_faenza['temp'].max(),
        df_ferrara['temp'].max(),
        df_bologna['temp'].max(),
        df_mantova['temp'].max(),
        df_piacenza['temp'].max(),
        df_milano['temp'].max(),
        df_asti['temp'].max(),
        df_torino['temp'].max()
    ]

    # temp_min 是一个存放每个城市最低温度的列表
    temp_min = [
        df_ravenna['temp'].min(),
        df_cesena['temp'].min(),
        df_faenza['temp'].min(),
        df_ferrara['temp'].min(),
        df_bologna['temp'].min(),
        df_mantova['temp'].min(),
        df_piacenza['temp'].min(),
        df_milano['temp'].min(),
        df_asti['temp'].min(),
        df_torino['temp'].min()
    ]

    fig, ax = plt.subplots()
    # ax.plot(dist, temp_max, 'ro')
    # axis 函数规定了 x 轴和 y 轴的取值范围
    plt.axis((0, 400, 15, 25))
    ax.plot(dist, temp_min, 'bo')

    plt.show()

stage2()
